<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.0.1">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/favicon.ico?v=7.0.1">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon.ico?v=7.0.1">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon.ico?v=7.0.1">


  <link rel="mask-icon" href="/images/favicon.ico?v=7.0.1" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.0.1',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="概述在windows做开发的时候，发布上线的时候，总是由于运行环境的差异，而在线上发现一些不必要的问题，因此需要将本地的开发环境与线上运行环境一致，就可以避免这些问题。在此技术上，考虑在window机器上搭建Linux环境，最常见的就是使用虚拟机。我使用的是vagrant来管理我的虚拟机，下面记录我搭建vagrant + vbox + centos的搭建过程。该过程同样适用于其他的linux版本。">
<meta name="keywords" content="Linux,Centos,Ubuntu,vagrant,Vbox">
<meta property="og:type" content="article">
<meta property="og:title" content="Vagrant 搭建 Linux">
<meta property="og:url" content="http://forkun.oschina.io/2017/05/18/Vagrant-搭建-Linux/index.html">
<meta property="og:site_name" content="forkun">
<meta property="og:description" content="概述在windows做开发的时候，发布上线的时候，总是由于运行环境的差异，而在线上发现一些不必要的问题，因此需要将本地的开发环境与线上运行环境一致，就可以避免这些问题。在此技术上，考虑在window机器上搭建Linux环境，最常见的就是使用虚拟机。我使用的是vagrant来管理我的虚拟机，下面记录我搭建vagrant + vbox + centos的搭建过程。该过程同样适用于其他的linux版本。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="">
<meta property="og:updated_time" content="2019-04-09T07:03:32.165Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Vagrant 搭建 Linux">
<meta name="twitter:description" content="概述在windows做开发的时候，发布上线的时候，总是由于运行环境的差异，而在线上发现一些不必要的问题，因此需要将本地的开发环境与线上运行环境一致，就可以避免这些问题。在此技术上，考虑在window机器上搭建Linux环境，最常见的就是使用虚拟机。我使用的是vagrant来管理我的虚拟机，下面记录我搭建vagrant + vbox + centos的搭建过程。该过程同样适用于其他的linux版本。">
<meta name="twitter:image" content="">






  <link rel="canonical" href="http://forkun.oschina.io/2017/05/18/Vagrant-搭建-Linux/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Vagrant 搭建 Linux | forkun</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">forkun</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://forkun.oschina.io/2017/05/18/Vagrant-搭建-Linux/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="forkun">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="forkun">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Vagrant 搭建 Linux

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2017-05-18 18:48:59" itemprop="dateCreated datePublished" datetime="2017-05-18T18:48:59+00:00">2017-05-18</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2019-04-09 07:03:32" itemprop="dateModified" datetime="2019-04-09T07:03:32+00:00">2019-04-09</time>
              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/服务器/" itemprop="url" rel="index"><span itemprop="name">服务器</span></a></span>

                
                
              
            </span>
          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h4 id="概述"><a href="#概述" class="headerlink" title="概述"></a>概述</h4><p>在windows做开发的时候，发布上线的时候，总是由于运行环境的差异，而在线上发现一些不必要的问题，因此需要将本地的开发环境与线上运行环境一致，就可以避免这些问题。<br>在此技术上，考虑在window机器上搭建Linux环境，最常见的就是使用虚拟机。我使用的是vagrant来管理我的虚拟机，下面记录我搭建vagrant + vbox + centos的搭建过程。<br>该过程同样适用于其他的linux版本。</p>
<a id="more"></a>
<h4 id="安装软件"><a href="#安装软件" class="headerlink" title="安装软件"></a>安装软件</h4><ul>
<li><p><strong><strong> 下载安装最新版本的<a href="https://www.vagrantup.com/downloads.html" target="_blank" rel="noopener">vagrant</a> </strong></strong><br>下载页面中，有各种版本下的软件下载，特别需要注意的是，下载的时候会没有下载进度，估计是被墙了，最好能翻墙下载该软件。</p>
</li>
<li><p><strong><strong> 下载安装最新版本的<a href="https://www.virtualbox.org/wiki/Downloads" target="_blank" rel="noopener">VirtualBox</a> </strong></strong><br>这个下载起来就比较快了，直接安装相应版本的vbox即可。<br>其实用vmware也可以来替代vbox的，但是个人感觉，vmware教vbox耗性能。自己试过，在同一个电脑上，安装vbox、vmware，vbox要比vmware快好多。</p>
</li>
<li><p><strong><strong> 安装好vagrant之后，软件会将<code>vagrant</code>命令，自动注册到系统变量，所以可以在命令行下直接执行<code>vagrant --version</code>命令测试是否安装成功。</strong></strong><br>截至目前位置，我们只是安装了容器软件，真正的linux系统并没有下载。通过vagrant来管理vbox的镜像包的话，有vagrant专用的box，自己下载的iso镜像文件，并不能用的。</p>
</li>
<li><p><strong><strong> 下载需要的版本的镜像包<a href="http://www.vagrantbox.es/" target="_blank" rel="noopener">box</a> </strong></strong><br>其实也可以不用手动下载镜像包，因为在<code>vagrant首先创建一个文件夹 box add</code>的时候，也可以指定box包，直接在远程下载的。但是这个在国内的下载速度，也是非常慢的，所以，最好提前下载好，在<code>vagrant add box</code>的时候，直接指定包的位置即可。</p>
</li>
</ul>
<h4 id="安装Linux系统"><a href="#安装Linux系统" class="headerlink" title="安装Linux系统"></a>安装Linux系统</h4><ul>
<li><p><strong><strong> 设定vbox默认虚拟电脑位置 </strong></strong><br>如果不设置虚拟电脑目录的话，系统安装linux的时候，会将linux文件，安装到vbox默认的windows系统盘。没必要无故占用系统盘空间。因此这里提前约束好。<br><img src="" alt="全局设定"></p>
</li>
<li><p><strong><strong> 创建vagrant的配置文件路径，该路径存放配置文件：Vagrantfile </strong></strong><br>该路径，是vbox内控制linux配置的文件：设置网络、设置共享文件夹、设置linux内存等。并且默认，该目录会被自动挂在到虚拟机内的linux中</p>
</li>
<li><p><strong><strong> 添加、初始化、运行 </strong></strong><br>进入上一步骤创建的路径目录。<br>在目录内使用：<code>vagrant box add linux_name box_path</code>命令，添加box包到vagrant。linux_name是虚拟机内部linux系统名，是唯一标识vagrant中一个虚拟机的名字。box_path设置为刚才下载的box文件的全路径。<br>在目录内使用：<code>vagrant init</code>命令，生成Vagrantfile配置文件。<br>执行：<code>vagrant up</code>命令，初次安装。安装成功后，该命令启动系统。<br>截止到目前为止，虚拟机中的系统已经安装完成。</p>
</li>
</ul>
<h4 id="常用的配置"><a href="#常用的配置" class="headerlink" title="常用的配置"></a>常用的配置</h4><ul>
<li><p><strong><strong> 网络设置 </strong></strong><br>关于网络设置，有3中设置：</p>
<ol>
<li><p>forwarded_port-端口映射：该模式是通过端口映射，来判别是访问主机，还是虚拟机。例如可以设置所有通过8989端口的访问，都映射到虚拟机内部的80端口：<code>config.vm.forwarded_port 80, 8989</code>。该方式优点是简单，容易设置。容易实现外网访问虚拟机。但是对于有多个端口需要映射配置的时候，就很吃力了。不可能每次增加一个端口，就增加一个映射关系的。</p>
</li>
<li><p>private_network-私有网路：我用的最多的也是这种网络，他只允许主机和虚拟机互通。同网络的其他主机，不能访问该虚拟机。<code>config.vm.network &quot;private_network&quot;, ip: &quot;192.168.33.188&quot;</code>，该配置开启的时候，在<code>vagrant reload</code>配置文件的时候，会在本机创建一个host-only的网络。其实这种方式就是虚拟机的<code>host-only</code>模式。</p>
</li>
<li><p>public_network-公用网络：和虚拟机一样，也是在内网的一个真实存在的机器，和其他主机可以相互访问。需要占用一个独立的IP：<code>config.vm.network &quot;public_network&quot;, ip: &quot;192.168.1.120&quot;</code></p>
</li>
</ol>
</li>
<li><p><strong><strong> SSH连接-登录-账号-启用root </strong></strong><br>默认的SSH登录的账号和密码是：<code>vagrant vagrant</code>。我们可以通过xshell客户端，通过vagrant账号进入系统然后：<code>sudo paswd root</code>命令，设置一个root账号的密码。<br>然后退出xshell，直接用root账号连接即可。</p>
</li>
<li><p><strong><strong> 共享文件夹的设置 </strong></strong><br>系统默认会将刚才创建的配置文件的目录挂载到虚拟机内部的：/vagrant目录。常用的配置选项有：</p>
<ol>
<li><p>create(boolean)–可选：默认为false，若配置为true，挂载到虚拟机上的目录若不存在则自动创建。</p>
</li>
<li><p>disabled(boolean)–可选：默认为false，若为true,则禁用该项挂载</p>
</li>
<li><p>owner(string):’www’–可选：虚拟机系统下文件所有者(确保系统下有该用户，否则会报错)，默认为vagrant</p>
</li>
<li><p>group(string):’www’–可选：虚拟机系统下文件所有组( (确保系统下有该用户组，否则会报错)，默认为vagrant</p>
</li>
<li><p>mount_options(array):[“dmode=775”,”fmode=664”]–可选：dmode配置目录权限，fmode配置文件权限  //默认权限777</p>
</li>
<li><p>type(string):–可选：指定文件共享方式，例如：’nfs’，vagrant默认根据系统环境选择最佳的文件共享方式</p>
<p>例如：需要如下配置：取消自动挂载的vagrant目录，将自动挂载的文件用户、用户组指定为：root，挂载的目录的权限设置为：775，可以做如下配置：</p>
<figure class="highlight stylus"><table><tr><td class="code"><pre><span class="line">config<span class="selector-class">.vm</span><span class="selector-class">.synced_folder</span> <span class="string">"e:/www"</span>, <span class="string">"/mnt/www"</span>, owner: <span class="string">"root"</span>, group: <span class="string">"root"</span>, mount_options: [<span class="string">"dmode=775"</span>,<span class="string">"fmode=664"</span>]</span><br><span class="line">config<span class="selector-class">.vm</span><span class="selector-class">.synced_folder</span> <span class="string">"."</span>, <span class="string">"/vagrant"</span>, disabled:true</span><br></pre></td></tr></table></figure>
</li>
</ol>
</li>
<li><p><strong><strong> 设置虚拟机内存 </strong></strong><br>将下面注释的这行打开，并且做如下设置：</p>
  <figure class="highlight ruby"><table><tr><td class="code"><pre><span class="line">config.vm.provider <span class="string">"virtualbox"</span> <span class="keyword">do</span> <span class="params">|vb|</span></span><br><span class="line">	<span class="comment">#vb.gui = true #这个选项可以不开启，控制vagrant启动的时候，是否启动vbox客户端。</span></span><br><span class="line">	vb.memory = <span class="string">"4096"</span>	<span class="comment">#占用内存大小。</span></span><br><span class="line">vb.memory = <span class="string">"4096"</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h4 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h4><ul>
<li>vagrant box add <name>新增box包</name></li>
<li>vagrant box list 列出当前vagrant中的包</li>
<li>vagrant box remove <name>删除vagrant中的包</name></li>
<li>vagrant init 初始化配置文件</li>
<li>vagrant up 启动系统</li>
<li>vagrant reload 重启系统，会重新载入Vagrantfile配置文件，一般有配置文件更改的时候，执行该命令。</li>
<li>vagrant halt 关闭系统</li>
</ul>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Linux/" rel="tag"># Linux</a>
          
            <a href="/tags/Centos/" rel="tag"># Centos</a>
          
            <a href="/tags/Ubuntu/" rel="tag"># Ubuntu</a>
          
            <a href="/tags/vagrant/" rel="tag"># vagrant</a>
          
            <a href="/tags/Vbox/" rel="tag"># Vbox</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/04/27/Mysql-常用语句技巧汇总/" rel="next" title="Mysql 常用语句技巧汇总">
                <i class="fa fa-chevron-left"></i> Mysql 常用语句技巧汇总
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/05/24/PHP写大文件（数据库正常，PHP内存溢出）/" rel="prev" title="PHP写大文件（数据库正常，PHP内存溢出）">
                PHP写大文件（数据库正常，PHP内存溢出） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">forkun</p>
              <div class="site-description motion-element" itemprop="description"></div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">25</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                      <a href="/categories/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">6</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">23</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-4"><a class="nav-link" href="#概述"><span class="nav-number">1.</span> <span class="nav-text">概述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#安装软件"><span class="nav-number">2.</span> <span class="nav-text">安装软件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#安装Linux系统"><span class="nav-number">3.</span> <span class="nav-text">安装Linux系统</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#常用的配置"><span class="nav-number">4.</span> <span class="nav-text">常用的配置</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#常用命令"><span class="nav-number">5.</span> <span class="nav-text">常用命令</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">forkun</span>

  

  
</div>









        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/src/utils.js?v=7.0.1"></script>

  <script src="/js/src/motion.js?v=7.0.1"></script>



  
  


  <script src="/js/src/affix.js?v=7.0.1"></script>

  <script src="/js/src/schemes/pisces.js?v=7.0.1"></script>



  
  <script src="/js/src/scrollspy.js?v=7.0.1"></script>
<script src="/js/src/post-details.js?v=7.0.1"></script>



  


  <script src="/js/src/next-boot.js?v=7.0.1"></script>


  

  

  

  


  


  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  

  

  

  

  

  

  

  

  

  

  

</body>
</html>
